package controller.textChat;

import java.io.IOException;

import controller.textChat.Interface.TextChatControl;

import model.textChat.Message;
import model.textChat.MessageReceiver;
import model.textChat.MessageRecordManager;
import model.textChat.MessageSender;
import model.textChat.TChatSocketManager;
import model.textChat.Interface.MessageObserver;
import model.textChat.Interface.MessageReceive;
import model.textChat.Interface.MessageSending;
import model.textChat.Interface.TChatSocketManage;

public class TextChatController implements TextChatControl 
{
	private MessageSending mSender;
	private MessageReceive mReceiver;
	private TChatSocketManage tcsManager;
	private MessageRecordManager mrManager;

	public TextChatController(MessageObserver observer) 
	{
		mSender = new MessageSender();
		mrManager = new MessageRecordManager();
		mReceiver = new MessageReceiver();
		mReceiver.register( observer );
		mReceiver.register( mrManager );
		
		tcsManager = new TChatSocketManager();
		tcsManager.register( mSender );
		tcsManager.register( mReceiver );
		
		mReceiver.start();
		tcsManager.start();
		
		System.out.println( "====== Client Text Chat Controller Constructed! ======" );
	}
	
	@Override
	public boolean sendMessage(String content) throws IOException 
	{
		return mSender.send( content );
	}

	@Override
	public void end()
	{
		tcsManager.end();
		mReceiver.end();
		mrManager.end();
	}

	@Override
	public void viewMessageRocored()
	{
		mrManager.viewMessageRecord();
	}

	@Override
	public Message readNextRocord()
	{
		return mrManager.readNextRecord();
	}

}
